<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;
use App\Services\OrderService;
use App\Services\CenterService;
use App\Services\CommonService;
use App\Services\MemberMsgService;

class OrderController extends Controller{
	public function list(Request $request){
		$username = Session::get('username');

		if(!$username){
			return redirect('/admin/login');
		}else{
			$order = new OrderService;
			$data['page_size_list'] = array(20,25,50,100);
			$data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 20;
			$data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';
			$data['username'] = $username;

			$data['orderlist'] = $order->getOrderList($data['page_size'],$data['keywords']);

			return view('admin.order.list',$data);
		}
	}

	public function view($id){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $order = new OrderService;
            $center = new CenterService;
            $common = new CommonService;
            $data['username'] = $username;
            $data['orderInfo'] = $order->getOrderInfo($id);
            $data['customer_list'] = array(
                'youzhengguonei'=>'邮政包裹',
                'ems'=>'EMS',
                'shunfeng'=>'顺丰快递',
                'shentong'=>'申通快递',
                'yuantong'=>'圆通快递',
                'zhongtong'=>'中通快递',
                'huitongkuaidi'=>'百世汇通',
                'yunda'=>'韵达快递',
                'zhaijisong'=>'宅急送',
                'tiantian'=>'天天快递',
                'jd'=>'京东',
            );
            
            if($data['orderInfo']['address_id'] > 0){
                $addressInfo = $center->getAddressInfoById($data['orderInfo']['address_id']);

                $province = $common->getPcInfoById($addressInfo->province);
                $addressInfo->province_name = $province->name;

                $city = $common->getPcInfoById($addressInfo->city);
                $addressInfo->city_name = $city->name;

                $county = $common->getPcInfoById($addressInfo->county);
                $addressInfo->county_name = $county->name;

                if(strlen($addressInfo->zip_code) < 6){
                    $addressInfo->zip_code = '0'.$addressInfo->zip_code;
                }

                $data['addressInfo'] = $addressInfo;
            }

            return view('admin.order.info',$data);
        }
    }

    public function close($id){
	    $order = new OrderService;
        $data['status'] = 60;
        $order->updateOrder($id,$data);
        return redirect()->back();

    }

    public function cancel($id){
        $order = new OrderService;
        $data['status'] = 0;
        $order->updateOrder($id,$data);
        return redirect()->back();
    }

    public function modifyLogistics(){
	    $order = new OrderService;
	    $membermsg = new MemberMsgService;

        date_default_timezone_set('PRC');

	    $order_id = intval($_POST['id']);
        $data['kd_stream_id'] = addslashes($_POST['stream_id']);
        $data['kd_ticket_id'] = $_msgData['ticket_id'] = addslashes($_POST['ticket_id']);
        $data['delivery_func'] = addslashes($_POST['delivery']);
        $data['customer']= $_msgData['customer']  = addslashes($_POST['customer']);
        $data['status'] = 40;
        $data['send_time'] = date('Y-m-d H:i:s');

        $_msgData['member_id'] = intval($_POST['member_id']);
        $_msgData['content'] = addslashes('<p>订单消息【系统消息】</p><p>尊敬的用户您好，您在【爆炸狗】官方平台上购买的教具已发货，发货单号：'
            .$_POST['ticket_id'].'请注意查收。</p>');
        $_msgData['add_date'] = date('Y-m-d H:i:s');


        $res1 = $order->updateOrder($order_id,$data);
        $res2 = $membermsg->insertmsg($_msgData);

        if($res1 && $res2){
            $_data = array('code'=>200);
        }else{
            $_data = array('code'=>400);
        }

        return response()->json($_data);
    }
}